<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/9/21
 * Time: 11:19
 */

namespace app\api\controller;

use language\Languagemsg;
use think\Db;


class Apilogin extends Base
{
    public function __construct()
    {
        $this->msg_language = new Languagemsg();
        $this->language = input('language') ? input('language') : 0;
    }
    
    /**
     * 发送手机验证码
     * @return \think\response\Json|void
     * phone       填写的手机号
     * type        发送场景   1 注册   2 重置/忘记密码   3 其它
     * ex          区号
     */
    public function getPhoneCode(){
        $params = input('');
    
        if(trim($params['phone'], ' ') == '') {
//            return json(['status' => 0, 'msg' => '请输入手机号']);
            return json(['status' => 0, 'msg' => $this->msg_language->writePhone[$this->language]]);
        }
        $userInfo = Db::table('up_u_user')->where('user_phone', $params['phone'])->find();
        if(!empty($userInfo) && isset($params['type']) && $params['type'] == 1) { //注册
//            return json(['status' => 0, 'msg' => '该手机号已注册']);
            return json(['status' => 0, 'msg' => $this->msg_language->phoneAreadyRegist[$this->language]]);
        }
        if(empty($userInfo) && isset($params['type']) && $params['type'] == 2) { //找回密码
//            return json(['status' => 0, 'msg' => '该手机号尚未注册']);
            return json(['status' => 0, 'msg' => $this->msg_language->phoneUnregist[$this->language]]);
        }
        $phone = '00' . $params['ex'] . $params['phone'];
        $code = GetRandCode();//6位数验证码
        $sendres = YzxSms($code, $phone, '');//发送验证码
        if($sendres['code'] != 0) {//发送不成功
            return json(['status' => 0, 'msg' => $sendres['msg']]);
        } else {
            $data = [
                'phone' => $params['phone'],//发送的手机号
                'code' => $code,//验证码
                'time' => time(),//验证码发送的时间戳
                'date_time' => date('Y-m-d H:i:s'),//日期格式
                'check_type' => 1
            ];
            $res = Db::table('up_phonecode')->insert($data);
            if($res) {
                return json(['status' => 200, 'msg' => $code]);
            }else{
//                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
                return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
            }
        }
    }
    
    /**
     * 发送邮箱验证码
     * @return \think\response\Json
     * email     邮箱
     * type      1 注册    2 忘记密码    3 其它
     */
    public function getEmailCode(){
        $params = input('');
        $userInfo = Db::table('up_u_user')->where('user_email', $params['email'])->find();
        // 注册
        if(isset($params['type']) && $params['type'] == 1 && $userInfo){
//            return json(['status' => 0, 'msg' => '该邮箱已注册']);
            return json(['status' => 0, 'msg' => $this->msg_language->emailAreadyRegist[$this->language]]);
        }
        // 找回密码
        if(isset($params['type']) && $params['type'] == 2 && !$userInfo){
//            return json(['status' => 0, 'msg' => '该邮箱尚未注册']);
            return json(['status' => 0, 'msg' => $this->msg_language->emailUnregist[$this->language]]);
        }
        $code = GetRandCode();//6位数验证码
        $result = emailCode($params['email'], $code);
        $result = json_decode($result, true);
    
        if($result['code'] == 200){
            $data = [
                'email' => $params['email'], //发送的邮箱
                'code' => $code,//验证码
                'time' => time(),//验证码发送的时间戳
                'date_time' => date('Y-m-d H:i:s'),//日期格式
                'check_type' => 2
            ];
            $res = Db::table('up_phonecode')->insert($data);
            if($res) {
                return json(['status' => 200, 'msg' => $code]);
            }else{
//                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
                return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
            }
        }else{
            return json(['status' => 0, 'msg' => $result['msg']]);
        }
    }
    
    /**
     * 手机号注册
     * @return \think\response\Json
     * country_code     国际区号
     * phone            手机号
     * code             验证码
     * pwd              密码
     * affirm_pwd       确认密码
     * invite_code      邀请码
     */
    public function phoneRegister(){
        $country_code = trim(input('country_code'), ' ');
        $phone = trim(input('phone'), ' ');
        $code = input('code');
        $pwd = input('pwd');
        $affirm_pwd = input('affirm_pwd');
        $invite_code = trim(input('invite_code'), ' ');
        
//        if(!$phone) return json(['status' => 0, 'msg' => '请输入手机号']);
//        if(!$code) return json(['status' => 0, 'msg' => '请填写验证码']);
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
//        if(!$affirm_pwd) return json(['status' => 0, 'msg' => '请确认密码']);
//        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => '密码不一致']);
        if(!$phone) return json(['status' => 0, 'msg' => $this->msg_language->writePhone[$this->language]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->msg_language->writeYZM[$this->language]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        if(!$affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->affirmpwd[$this->language]]);
        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->pwdNotSame[$this->language]]);
        $user_info = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if($user_info) return json(['status' => 0, 'msg' => '该手机号已注册']);
        if($user_info) return json(['status' => 0, 'msg' => $this->msg_language->phoneAreadyRegist[$this->language]]);
        // 验证码
        $yzm = Db::table('up_phonecode')->where('phone', $phone)->where('check_type', 1)->order('time desc')->limit(1)->find();
//        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
        // 5分钟超时
        $now = time();
//        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
        
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $insert = [
            'user_username' => $phone,
            'user_phone' => $phone,
            'user_password' => md5($pwd),
            'user_country_code' => $country_code,
            'user_time' => date('Y-m-d H:i:s'),
            'user_token' => $token,
            'user_last_login_time' => date('Y-m-d H:i:s')
        ];
        if(isset($invite_code) && $invite_code != '') $insert['user_invite_other_code'] = $invite_code;
        $res = Db::table('up_u_user')->insertGetId($insert);
        if($res){
            // 更新用户邀请码
            $my_invite_code = str_pad($res, 7, "0", STR_PAD_LEFT);
            Db::table('up_u_user')->where('user_id', $res)->update(['user_invite_my_code' => $my_invite_code]);
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            $user = Db::table('up_u_user')->where('user_id', $res)->find();
            // TODO:融云
            $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $user]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 邮箱注册
     * @return \think\response\Json
     * email       邮箱
     * code        验证码
     * pwd         密码
     * affirm_pwd  确认密码
     * invite_code 邀请码
     */
    public function emailRegister(){
        $email = input('email');
        $code = input('code');
        $pwd = input('pwd');
        $affirm_pwd = input('affirm_pwd');
        $invite_code = trim(input('invite_code'), ' ');
        
//        if(!$email) return json(['status' => 0, 'msg' => '请填写邮箱']);
//        if(!$code) return json(['status' => 0, 'msg' => '请填写验证码']);
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
//        if(!$affirm_pwd) return json(['status' => 0, 'msg' => '请确认密码']);
//        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => '密码不一致']);
        if(!$email) return json(['status' => 0, 'msg' => $this->msg_language->writeEmail[$this->language]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->msg_language->writeYZM[$this->language]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        if(!$affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->affirmpwd[$this->language]]);
        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->pwdNotSame[$this->language]]);
        $user_info = Db::table('up_u_user')->where('user_email', $email)->find();
//        if($user_info) return json(['status' => 0, 'msg' => '该邮箱已注册']);
        if($user_info) return json(['status' => 0, 'msg' => $this->msg_language->emailAreadyRegist[$this->language]]);
        // 验证码
        $yzm = Db::table('up_phonecode')->where('email', $email)->where('check_type', 2)->order('time desc')->limit(1)->find();
//        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
        // 5分钟超时
        $now = time();
//        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
        
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $insert = [
            'user_username' => $email,
            'user_email' => $email,
            'user_password' => md5($pwd),
            'user_time' => date('Y-m-d H:i:s'),
            'user_token' => $token,
            'user_last_login_time' => date('Y-m-d H:i:s')
        ];
        if(isset($invite_code) && $invite_code != '') $insert['user_invite_other_code'] = $invite_code;
        $res = Db::table('up_u_user')->insertGetId($insert);
        if($res){
            // 更新用户邀请码
            $my_invite_code = str_pad($res, 7, "0", STR_PAD_LEFT);
            Db::table('up_u_user')->where('user_id', $res)->update(['user_invite_my_code' => $my_invite_code]);
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
            $user = Db::table('up_u_user')->where('user_id', $res)->find();
            // TODO:融云
            $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $user]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 手机号、密码登录
     * @return \think\response\Json
     * phone      手机号
     * pwd        密码
     */
    public function phoneLogin(){
        $phone = trim(input('phone'), ' ');
        $pwd = input('pwd');
        
//        if(!$phone) return json(['status' => 0, 'msg' => '请输入手机号']);
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
        if(!$phone) return json(['status' => 0, 'msg' => $this->msg_language->writePhone[$this->language]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        $login = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if(!$login) return json(['status' => 0, 'msg' => '该手机号尚未注册']);
        if(!$login) return json(['status' => 0, 'msg' => $this->msg_language->phoneUnregist[$this->language]]);
        $password = md5($pwd);
//        if($password != $login['user_password']) return json(['status' => 0, 'msg' => '密码错误']);
//        if($login['user_status'] == 2) return json(['status' => 0, 'msg' => '该账号当前已被禁用，如有问题请联系客服']);
        if($password != $login['user_password']) return json(['status' => 0, 'msg' => $this->msg_language->pwdErr[$this->language]]);
        if($login['user_status'] == 2) return json(['status' => 0, 'msg' => $this->msg_language->frost[$this->language]]);
        $token = md5(date('YmdHis') . rand(100000, 999999));
        
        $update = [
            'user_token' => $token,
            'user_last_login_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        if($res){
            $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
            // TODO:融云
            $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $user]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 邮箱、密码登录
     * @return \think\response\Json
     * email      邮箱
     * pwd        密码
     */
    public function emailLogin(){
        $email = trim(input('email'), ' ');
        $pwd = input('pwd');
        
//        if(!$email) return json(['status' => 0, 'msg' => '请填写邮箱']);
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
        if(!$email) return json(['status' => 0, 'msg' => $this->msg_language->writeEmail[$this->language]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        $login = Db::table('up_u_user')->where('user_email', $email)->find();
//        if(!$login) return json(['status' => 0, 'msg' => '该邮箱尚未注册']);
        if(!$login) return json(['status' => 0, 'msg' => $this->msg_language->emailUnregist[$this->language]]);
        $password = md5($pwd);
//        if($password != $login['user_password']) return json(['status' => 0, 'msg' => '密码错误']);
//        if($login['user_status'] == 2) return json(['status' => 0, 'msg' => '该账号当前已被禁用，如有问题请联系客服']);
        if($password != $login['user_password']) return json(['status' => 0, 'msg' => $this->msg_language->pwdErr[$this->language]]);
        if($login['user_status'] == 2) return json(['status' => 0, 'msg' => $this->msg_language->frost[$this->language]]);
        $token = md5(date('YmdHis') . rand(100000, 999999));
        
        $update = [
            'user_token' => $token,
            'user_last_login_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_u_user')->where('user_email', $email)->update($update);
        if($res){
            $user = Db::table('up_u_user')->where('user_email', $email)->find();
            // TODO:融云
            $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $user]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 第三方登录第一步---验证token
     * @return \think\response\Json
     * login_type      1 apple   2 微信
     * third_token     微信传token,apple传ID
     */
    public function thirdLoginF(){
        $login_type = input('login_type'); // 1 apple   2 微信
        $third_token = input('third_token'); // 微信传token，apple传ID
        if($login_type == 1){
            $field = 'user_apple_token';
        }else{
            $field = 'user_wx_token';
        }
        $first_login = Db::table('up_u_user')->where($field, $third_token)->find();
        if($first_login){ // 有记录
            if($first_login['user_status'] == 2){
//                return json(['status' => 0, 'msg' => '该账号当前已被禁用，如有问题请联系客服']);
                return json(['status' => 0, 'msg' => $this->msg_language->frost[$this->language]]);
            }
            $token = md5(date('YmdHis') . rand(100000, 999999));
            $update = [
                'user_last_login_time' => date('Y-m-d H:i:s'),
                'user_token' => $token
            ];
            $res = Db::table('up_u_user')->where('user_id', $first_login['user_id'])->update($update);
            if($res){
                $user = Db::table('up_u_user')->where('user_id', $first_login['user_id'])->find();
                // todo:融云
                $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
                $user['rytoken'] = $rytoken['token'];
                return json(['status' => 200, 'msg' => $user]);
            }else{
//                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
                return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
            }
        }else{
            // 第一次登录，先绑定手机
//            return json(['status' => 201, 'msg' => '首次登录，请先绑定手机号']);
            return json(['status' => 201, 'msg' => $this->msg_language->bandPhone[$this->language]]);
        }
    }
    
    /**
     * 第三方登录第二步---绑定手机
     * @return \think\response\Json
     * phone           手机号
     * code            验证码
     * login_type      第三方登录类型   1 apple   2 微信
     * third_token     微信传token,apple传ID
     * third_nickname  微信第三方昵称
     * third_image     微信第三方头像
     */
    public function thirdLoginS(){
        $phone = trim(input('phone'), ' ');
        $code = input('code');
        $login_type = input('login_type');
        $third_token = input('third_token');
        $third_nickname = input('third_nickname');
        $third_image = input('third_image');
        if($login_type == 1){
            $field = 'user_apple_token';
        }else{
            $field = 'user_wx_token';
        }
        
//        if(!$phone) return json(['status' => 0, 'msg' => '请输入手机号']);
//        if(!$code) return json(['status' => 0, 'msg' => '请填写验证码']);
        if(!$phone) return json(['status' => 0, 'msg' => $this->msg_language->writePhone[$this->language]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->msg_language->writeYZM[$this->language]]);
        // 验证码
        $yzm = Db::table('up_phonecode')->where('phone', $phone)->where('check_type', 1)->order('time desc')->limit(1)->find();
//        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
        // 5分钟超时
        $now = time();
//        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
        
        $exist = Db::table('up_u_user')->where('user_phone', $phone)->find();
        $token = md5(date('YmdHis') . rand(100000, 999999));
        if($exist){ // 如果有记录，则直接登录
//            if($exist['user_status'] == 2) return json(['status' => 0, 'msg' => '该账号当前已被禁用，如有问题请联系客服']);
            if($exist['user_status'] == 2) return json(['status' => 0, 'msg' => $this->msg_language->frost[$this->language]]);
            $update = [
                'user_token' => $token,
                'user_last_login_time' => date('Y-m-d H:i:s'),
                $field => $third_token
            ];
            if($login_type == 2){
                $update['user_wx_image'] = $third_image;
                $update['user_wx_nickname'] = $third_nickname;
            }
            if(empty($exist['user_username'])) $update['user_username'] = $third_nickname;
            if(empty($exist['user_image'])) $update['user_image'] = $third_image;
            $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
            if($res){
                $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
                //修改验证码状态
                Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
                // todo:融云
                $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
                $user['rytoken'] = $rytoken['token'];
                return json(['status' => 200, 'msg' => $user]);
            }else{
//                return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
                return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
            }
        }else{
//            return json(['status' => 202, 'msg' => '请填写密码']);
            return json(['status' => 202, 'msg' => $this->msg_language->writepwd[$this->language]]);
        }
    }
    
    /**
     * 第三方登录第三步---输入密码
     * @return \think\response\Json
     * phone           手机号
     * login_type      登录方式  1 apple  2 微信
     * third_token     第三方token
     * third_nickname  第三方昵称，apple传空
     * third_image     第三方头像，apple传空
     * country_code    国家区号
     * pwd             密码
     * affirm_pwd      确认密码
     */
    public function thirdLoginT(){
        $phone = input('phone');
        $login_type = input('login_type');
        $third_token = input('third_token');
        $third_nickname = input('third_nickname');
        $third_image = input('third_image');
        $country_code = input('country_code');
        $pwd = input('pwd');
        $affirm_pwd = input('affirm_pwd');
        if($login_type == 1){
            $field = 'user_apple_token';
        }else{
            $field = 'user_wx_token';
        }
        
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
//        if(!$affirm_pwd) return json(['status' => 0, 'msg' => '请确认密码']);
//        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => '密码不一致']);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        if(!$affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->affirmpwd[$this->language]]);
        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->pwdNotSame[$this->language]]);
        
        $token = md5(date('YmdHis') . rand(100000, 999999));
        $insert = [
            'user_phone' => $phone,
            'user_token' => $token,
            $field => $third_token,
            'user_time' => date('Y-m-d H:i:s'),
            'user_last_login_time' => date('Y-m-d H:i:s'),
            'user_country_code' => $country_code,
            'user_password' => md5($pwd)
        ];
        if($login_type == 2){
            $insert['user_wx_image'] = $third_image;
            $insert['user_wx_nickname'] = $third_nickname;
            $insert['user_image'] = $third_image;
            $insert['user_username'] = $third_nickname;
        }
        $res = Db::table('up_u_user')->insertGetId($insert);
        if($res){
            // 更新用户邀请码
            $my_invite_code = str_pad($res, 7, "0", STR_PAD_LEFT);
            Db::table('up_u_user')->where('user_id', $res)->update(['user_invite_my_code' => $my_invite_code]);
            $user = Db::table('up_u_user')->where('user_phone', $phone)->find();
            // todo:融云
            $rytoken = $this->rongyun($user['user_id'],'who',$user['user_image']);
            $user['rytoken'] = $rytoken['token'];
            return json(['status' => 200, 'msg' => $user]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 重置密码
     * @return \think\response\Json
     * phone        手机号
     * pwd          密码
     * code         验证码
     * affirm_pwd   确认密码
     */
    public function resetPassword(){
        $phone = trim(input('phone'), ' ');
        $pwd = input('pwd');
        $affirm_pwd = input('affirm_pwd');
        $code = input('code');
//        if(!$phone) return json(['status' => 0, 'msg' => '请输入手机号']);
//        if(!$code) return json(['status' => 0, 'msg' => '请填写验证码']);
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
//        if(!$affirm_pwd) return json(['status' => 0, 'msg' => '请确认密码']);
//        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => '密码不一致']);
        if(!$phone) return json(['status' => 0, 'msg' => $this->msg_language->writePhone[$this->language]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->msg_language->writeYZM[$this->language]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        if(!$affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->affirmpwd[$this->language]]);
        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->pwdNotSame[$this->language]]);
        
        // 账号
        $exist = Db::table('up_u_user')->where('user_phone', $phone)->find();
//        if(empty($exist)) return json(['status' => 0, 'msg' => '该手机号尚未注册']);
        if(empty($exist)) return json(['status' => 0, 'msg' => $this->msg_language->phoneUnregist[$this->language]]);
        // 验证码
        $yzm = Db::table('up_phonecode')->where('phone', $phone)->where('check_type', 1)->order('time desc')->limit(1)->find();
//        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
        // 5分钟超时
        $now = time();
//        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
        $update = [
            'user_update_time' => date('Y-m-d H:i:s'),
            'user_password' => md5($pwd)
        ];
        $res = Db::table('up_u_user')->where('user_phone', $phone)->update($update);
        if($res){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
//            return json(['status' => 200, 'msg' => '重置成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->resetSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 邮箱重置密码
     * @return \think\response\Json
     * email        手机号
     * pwd          密码
     * code         验证码
     * affirm_pwd   确认密码
     */
    public function resetPasswordEmail(){
        $email = trim(input('email'), ' ');
        $pwd = input('pwd');
        $affirm_pwd = input('affirm_pwd');
        $code = input('code');
//        if(!$phone) return json(['status' => 0, 'msg' => '请输入手机号']);
//        if(!$code) return json(['status' => 0, 'msg' => '请填写验证码']);
//        if(!$pwd) return json(['status' => 0, 'msg' => '请填写密码']);
//        if(!$affirm_pwd) return json(['status' => 0, 'msg' => '请确认密码']);
//        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => '密码不一致']);
        if(!$email) return json(['status' => 0, 'msg' => $this->msg_language->writeEmail[$this->language]]);
        if(!$code) return json(['status' => 0, 'msg' => $this->msg_language->writeYZM[$this->language]]);
        if(!$pwd) return json(['status' => 0, 'msg' => $this->msg_language->writepwd[$this->language]]);
        if(!$affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->affirmpwd[$this->language]]);
        if($pwd !== $affirm_pwd) return json(['status' => 0, 'msg' => $this->msg_language->pwdNotSame[$this->language]]);
        
        // 账号
        $exist = Db::table('up_u_user')->where('user_email', $email)->find();
//        if(empty($exist)) return json(['status' => 0, 'msg' => '该手机号尚未注册']);
        if(empty($exist)) return json(['status' => 0, 'msg' => $this->msg_language->phoneUnregist[$this->language]]);
        // 验证码
        $yzm = Db::table('up_phonecode')->where('email', $email)->where('check_type', 2)->order('time desc')->limit(1)->find();
//        if($code != $yzm['code']) return json(['status' => 0, 'msg' => '验证码错误']);
        if($code != $yzm['code']) return json(['status' => 0, 'msg' => $this->msg_language->yzmErr[$this->language]]);
        // 5分钟超时
        $now = time();
//        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => '验证码失效']);
//        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => '验证码已使用']);
        if($now - $yzm['time'] > 300) return json(['status' => 0, 'msg' => $this->msg_language->yzmLose[$this->language]]);
        if($yzm['status'] == 1) return json(['status' => 0, 'msg' => $this->msg_language->yzmUsed[$this->language]]);
        $update = [
            'user_update_time' => date('Y-m-d H:i:s'),
            'user_password' => md5($pwd)
        ];
        $res = Db::table('up_u_user')->where('user_email', $email)->update($update);
        if($res){
            //修改验证码状态
            Db::table('up_phonecode')->where('id', $yzm['id'])->setField('status', 1);
//            return json(['status' => 200, 'msg' => '重置成功']);
            return json(['status' => 200, 'msg' => $this->msg_language->resetSuccess[$this->language]]);
        }else{
//            return json(['status' => 0, 'msg' => '系统异常，请稍后重试']);
            return json(['status' => 0, 'msg' => $this->msg_language->systemErr[$this->language]]);
        }
    }
    
    /**
     * 用户协议
     * @return \think\response\Json
     */
    public function user_agreement(){
        $agreement = Db::table('up_other')->where('other_id', 1)->value('other_user_agreement');
        if($agreement){
            return json(['status' => 200, 'msg' => $agreement]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 用户隐私协议
     * @return \think\response\Json
     */
    public function user_ys(){
        $ys = Db::table('up_other')->where('other_id', 2)->value('other_ys_agreement');
        if($ys){
            return json(['status' => 200, 'msg' => $ys]);
        }else{
            return json(['status' => 200, 'msg' => []]);
        }
    }
    
    /**
     * 国际区号
     * @return \think\response\Json
     * keyword    搜索关键词
     */
    public function countryCode(){
        $keyword = input('keyword');
        $where = [];
        if(isset($keyword) && $keyword != ''){
            $where['country_name'] = ['like', '%' . $keyword . '%'];
        }
        $code = Db::table('up_country_code')
            ->field('country_id, country_name, country_code')
            ->where($where)
            ->select();
        return json(['status' => 200, 'msg' => $code]);
    }
    
    /**
     * 判断用户15天内是否登录过
     * @return \think\response\Json
     * user_id   用户ID
     */
    public function anewLogin(){
        $user_id = input('user_id');
        $anew_time = strtotime(date('Y-m-d H:i:s', strtotime('-15 days')));
        $user_last_login_time = Db::table('up_u_user')->where('user_id', $user_id)->value('user_last_login_time');
        if(strtotime($user_last_login_time) < $anew_time){
//            return json(['status' => 201, 'msg' => '15天内未登录，重新登录']);
            return json(['status' => 201, 'msg' => $this->msg_language->relogin[$this->language]]);
        }else{
            Db::table('up_u_user')->where('user_id', $user_id)->update(['user_last_login_time' => date('Y-m-d H:i:s')]);
//            return json(['status' => 202, 'msg' => '15天内登录过，自动登录']);
            return json(['status' => 202, 'msg' => $this->msg_language->autoLogin[$this->language]]);
        }
    }
    
    /**
     * 获取省市区的三维数组
     * @return \think\response\Json
     */
    public function getArea()
    {
        $provinceLists = Db::name('area')->where('level', 1)->select(); // 省
        $cityLists = Db::name('area')->where('level', 2)->select(); // 市
        $districtLists = Db::name('area')->where('level', 3)->select(); // 区
        foreach($cityLists as $k => $city) {
            foreach($districtLists as $kk => $district) {
                if($city['district_id'] == $district['pid']) {
                    $cityLists[$k]['qu'][] = $district;
                }
            }
        }
        foreach($provinceLists as $key => $province) {
            foreach($cityLists as $ke => $city) {
                if($province['district_id'] == $city['pid']) {
                    $provinceLists[$key]['city'][] = $city;
                }
            }
        }
        return json(['status' => 200, 'msg' => $provinceLists]);
    }
}